package com.zxy.ziems.server.device.convertor;

import com.zxy.ziems.common.pojo.MeasureDataBean;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmTypeEntity;
import com.zxy.ziems.server.device.pojo.bo.Coordinate;
import com.zxy.ziems.server.device.pojo.dto.*;
import com.zxy.ziems.server.device.pojo.entity.*;
import com.zxy.ziems.server.device.pojo.enums.DeviceStatus;
import com.zxy.ziems.server.device.pojo.vo.*;
import com.zxy.ziems.server.domain.Org;
import com.zxy.ziems.server.scene.pojo.entity.SceneEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author chdzq
 * @create 2023/3/22
 */
@Mapper(uses = {DeviceStatus.class})
public interface DeviceConvertor {

    DeviceConvertor INSTANCE = Mappers.getMapper(DeviceConvertor.class);


    //设备类型
    DeviceTypeEntity typeDTOConvert2DO(DeviceTypeDTO dto);

    DeviceTypeVO typeDOConvert2VO(DeviceTypeEntity entity);

    //设备操作
    List<DeviceTypeVO> typeDOListConvert2VOList(List<DeviceTypeEntity> list);

    //设备
    @Mapping(target = "relativeX", source = "coordinate.x")
    @Mapping(target = "relativeY", source = "coordinate.y")
    @Mapping(target = "relativeZ", source = "coordinate.z")
    @Mapping(target = "typeCode", source = "type")
    DeviceEntity deviceDTOConvert2DO(DeviceDTO<?> dto);

    @Mapping(target = "id", source = "entity.id")
    @Mapping(target = "name", source = "entity.name")
    @Mapping(target = "code", source = "entity.code")
    @Mapping(target = "type", source = "type")
    @Mapping(target = "coordinate", source = "entity")
    DeviceVO<Object> deviceDOConvert2VO(DeviceEntity entity,
                                        DeviceTypeEntity type,
                                        SceneEntity building,
                                        SceneEntity floor,
                                        Org org
    );

    DeviceVO<Object> deviceDOConvert2VO(DeviceEntity entity);

    @Mapping(target = "x", source = "entity.relativeX")
    @Mapping(target = "y", source = "entity.relativeY")
    @Mapping(target = "z", source = "entity.relativeZ")
    @Mapping(target = "alarmStatus", source = "entity.alarmStatus")
    DeviceAlarmVO deviceDOConvert2AlarmVO(DeviceEntity entity);


    @Mapping(target = "x", source = "relativeX")
    @Mapping(target = "y", source = "relativeY")
    @Mapping(target = "z", source = "relativeZ")
    Coordinate deviceDOConvert2Coordinate(DeviceEntity entity);

    DeviceTypePropertyEntity deviceTypePropertyDTOConvert2DO(DeviceTypePropertyDTO dto);

    @Mapping(target = "id", source = "entity.id")
    @Mapping(target = "name", source = "entity.name")
    DevicePropertyVO deviceTypePropertyDOConvert2VO(DeviceTypePropertyEntity entity,
                                                    DataTypeEntity dataType,
                                                    DataUnitEntity unit,
                                                    Integer metaStatus);


    @Mapping(target = "advanced", ignore = true)
    DevicePropertyEntity devicePropertyDTOConvert2DO(DevicePropertyDTO dto);

    @Mapping(target = "id", source = "entity.id")
    @Mapping(target = "name", source = "entity.name")
    @Mapping(target = "advanced", ignore = true)
    DevicePropertyVO devicePropertyDOConvert2VO(DevicePropertyEntity entity,
                                                DataTypeEntity dataType,
                                                DataUnitEntity unit,
                                                DataUnitEntity dataUnit,
                                                Integer metaStatus);

    //协议管理
    ProtocolEntity protocolDTOConvert2DO(ProtocolDTO dto);

    ProtocolVO protocolDOConvert2VO(ProtocolEntity entity);

    List<ProtocolVO> protocolDOListConvert2VOList(List<ProtocolEntity> entities);

    //采集器
    CollectorEntity collectorDTOConvert2DO(CollectorDTO dto);

    CollectorVO collectorDOConvert2VO(CollectorEntity entity);

    List<CollectorVO> collectorDOListConvert2VOList(List<CollectorEntity> entities);

    //通信通道管理
    ChannelEntity communicationChannelDTOConvert2DO(ChannelDTO dto);

    @Mapping(target = "id", source = "entity.id")
    @Mapping(target = "name", source = "entity.name")
    @Mapping(target = "ip", source = "entity.ip")
    @Mapping(target = "port", source = "entity.port")
    ChannelVO communicationChannelDOConvert2VO(ChannelEntity entity, CollectorEntity collector, ProtocolEntity protocol, DeviceTypeEntity deviceType);


    //设备属性配置告警类型
    DevicePropertyAlarmEntity devicePropertyAlarmDTOConvert2DO(DevicePropertyAlarmDTO dto, String deviceDataTypeId);

    @Mapping(target = "id", source = "entity.id")
    @Mapping(target = "description", source = "entity.description")
    @Mapping(target = "devicePropertyName", source = "deviceProperty.name")
    @Mapping(target = "alarmTypeName", source = "alarmType.name")
    DevicePropertyAlarmVO devicePropertyAlarmDOConvert2VO(DevicePropertyAlarmEntity entity, DevicePropertyEntity deviceProperty, AlarmTypeEntity alarmType);


    // 数据单位
    List<DataUnitVO> unitDOListConvert2VOList(List<DataUnitEntity> dataUnitEntityList);

    DataUnitVO dataUnitDOListConvert2VO(DataUnitEntity dataUnitEntity);

    // 数据分类
    DataTypeVO dataTypeDOListConvert2VO(DataTypeEntity dataTypeEntity);

    List<DataTypeVO> dataTypDOListConvert2VOList(List<DataTypeEntity> dataTypeEntityList);

    @Mapping(target = "dataTypeVal", source = "measureValue")
    DeviceExtendEntity measureDataBean2DeviceExtendEntity(MeasureDataBean bean);

    DeviceExtendVO deviceExtendEntity2DeviceExtendVo(DeviceExtendEntity m, String dataTypeCode);
}
